package com.aegean.onlinebook.service.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.aegean.onlinebook.model.BigType;
import com.aegean.onlinebook.model.ServiceValue;
import com.aegean.onlinebook.model.SmallType;
import com.aegean.onlinebook.service.dao.ServiceDao;

@Repository
@Transactional
public class ServiceDaoImpl implements ServiceDao {
	@Resource
	SessionFactory factory;

	@Override
	public void insertService(ServiceValue service) {
		// TODO Auto-generated method stub
		Session session = factory.getCurrentSession();
		try {
			if (service != null) {
				session.save(service);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	public boolean searchService(ServiceValue service) {
		Session session = null;
		ServiceValue one;
		try {
			session = factory.getCurrentSession();
			if (service != null) {
				Criteria c = session.createCriteria(ServiceValue.class);
				c.add(Restrictions.eq("user_Login", service.getUser_Login()));
				c.add(Restrictions.eq("user_Password",
						service.getUser_Password()));
				one = (ServiceValue) c.uniqueResult();
				if (one == null) {
					return false;
				}
				return true;
			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public int searchBigTypeCount() {
		Session session = null;
		try {
			session = factory.getCurrentSession();
			Criteria cr = session.createCriteria(BigType.class);
			cr.setProjection(Projections.projectionList());
			cr.setCacheable(true);
			int count = (Integer) cr.setProjection(Projections.rowCount())
					.uniqueResult();
			cr.setCacheable(true);
			return count;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public List<BigType> searchPartBigTypes(int start, int limit) {
		Session session = null;
		List<BigType> bigtypes = null;
		try {
			session = factory.getCurrentSession();
			Criteria cr = session.createCriteria(BigType.class);
			cr.setCacheable(true);
			cr.setFirstResult(start);
			cr.setMaxResults(limit);
			bigtypes = (List<BigType>) cr.list();
			return bigtypes;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean searchBigType(String name) {
		Session session = null;
		try {
			session = factory.getCurrentSession();
			Criteria cr = session.createCriteria(BigType.class);
			cr.add(Restrictions.eq("big_Name", name));
			cr.setCacheable(true);
			BigType o = (BigType) cr.uniqueResult();
			if (o == null) {
				return false;
			} else {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public boolean searchSmallType(String name) {
		Session session = null;
		try {
			session = factory.getCurrentSession();
			Criteria cr = session.createCriteria(SmallType.class);
			cr.add(Restrictions.eq("small_Name", name));
			cr.setCacheable(true);
			SmallType o = (SmallType) cr.uniqueResult();
			if (o == null) {
				return false;
			} else {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean addBigType(BigType one) {
		Session session = null;
		try {
			session = factory.getCurrentSession();
			session.save(one);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public boolean addSmallType(SmallType one,int bigType_Id) {
		Session session = null;
		try {
			session = factory.getCurrentSession();
			Query q=session.createSQLQuery("insert into smalltype(SMALL_NAME,SMALL_STATE,SMALL_DATE,BIG_ID) values(?,?,?,?)");
			q.setString(0,one.getSmall_Name());
			q.setString(1,one.getSmall_State());
			q.setDate(2,one.getSmall_Date());
			q.setInteger(3,bigType_Id);
			q.executeUpdate();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public boolean editBigType(BigType one) {
		Session session = null;
		BigType big;
		try {
			session = factory.getCurrentSession();
			Criteria cr = session.createCriteria(BigType.class);
			big = (BigType) cr.add(
					Restrictions.eq("big_Name", one.getBig_Name()))
					.uniqueResult();
			big.setBig_Date(one.getBig_Date());
			big.setBig_State(one.getBig_State());
			big.setBig_Small(one.getBig_Small());
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public boolean editSmallType(SmallType one) {
		Session session = null;
		SmallType small;
		try {
			session = factory.getCurrentSession();
			Criteria cr = session.createCriteria(BigType.class);
			small = (SmallType) cr.add(
					Restrictions.eq("small_Name", one.getSmall_Name()))
					.uniqueResult();
			small.setSmall_Date(one.getSmall_Date());
			small.setSmall_State(one.getSmall_State());
			small.setSmall_Goods(one.getSmall_Goods());
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean deleteBigType(String[] big_Name) {
		Session session = null;
		try {
			session = factory.getCurrentSession();
			for (int i = 0; i < big_Name.length; i++) {
				BigType big = new BigType();
				Criteria cr = session.createCriteria(BigType.class);
				big = (BigType) cr
						.add(Restrictions.eq("big_Name", big_Name[i]))
						.uniqueResult();
				session.delete(big);
				session.flush();
				session.clear();
			}
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public int searchSmallTypeCount(String bigName) {
		Session session = null;
		BigType one;
		try {
			session = factory.getCurrentSession();
			if (bigName == null || bigName.trim().length() == 0) {
				Criteria cr =session.createCriteria(SmallType.class);
				cr.setCacheable(true);
				return cr.list().size();
			} else {
				Criteria cr =session.createCriteria(BigType.class);
				cr.setCacheable(true);
				cr.add(Restrictions.eq("big_Name", bigName));
				one=(BigType) cr.uniqueResult();
				return one.getBig_Small().size();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public List<SmallType> searchPartSmallTypes(String bigName, int start,
			int limit) {
		Session session = null;
		List<SmallType> smalltypes;
		BigType one;
		try {
			session = factory.getCurrentSession();
			if (bigName == null || bigName.trim().length() == 0) {
				String sql = "select * from smalltype";
				Query q = session.createSQLQuery(sql).addEntity(SmallType.class);
				q.setCacheable(true);
				smalltypes = q.list();
				return smalltypes;
			} else {
				Criteria cr = session.createCriteria(BigType.class);
				cr.add(Restrictions.eq("big_Name", bigName));
				one = (BigType) cr.uniqueResult();
				cr.setCacheable(true);
				String sql = "select * from smalltype where big_id ="
						+ one.getBig_Id();
				Query q = session.createSQLQuery(sql).addEntity(SmallType.class);
				q.setFirstResult(start);
				q.setMaxResults(limit);
				q.setCacheable(true);
				smalltypes = q.list();
				return smalltypes;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
